package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.util.List;

import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.Device_InDao;
import com.zretc.device.dao.Device_OutDao;
import com.zretc.device.entity.Device_In;
import com.zretc.device.entity.Device_Out;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Device_OutFindDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtInput;
	private JTextField txtout_id;
	private JTextField txtdevice_code;
	private JTextField txtdepartment;
	private JTextField txtout_date;
	private JTextField txtout_status;
	private JTextField txtout_num;
	private JTextField txtout_person;
	private JTextField txttaker;
	private JTable table;
	private String out_id;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_OutFindDialog dialog = new Device_OutFindDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_OutFindDialog() {
		setBounds(100, 100, 938, 585);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(52, 208, 809, 277);
		contentPanel.add(scrollPane);
		
		JLabel label_1 = new JLabel("请选择查询方式");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(52, 27, 181, 38);
		contentPanel.add(label_1);

		JComboBox txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "出库单号", "设备编号", "出库日期" }));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(258, 33, 130, 30);
		contentPanel.add(txtCB);

		JLabel label = new JLabel("请输入查询信息");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(52, 81, 186, 30);
		contentPanel.add(label);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setColumns(10);
		txtInput.setBounds(239, 81, 317, 30);
		contentPanel.add(txtInput);

		JButton button = new JButton("查询");
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取复选框里的值
				String opString = txtCB.getSelectedItem() + "";
				// 获取文本框里的值
				String txt = txtInput.getText();
				if (txt.contentEquals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "查询内容不能为空");
				} else {
					if (opString.equals("出库单号")) {
						// 调用方法
						int outId = Integer.parseInt(txt);
						Device_Out dout = fillTableById(scrollPane, outId);
						if (dout == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该单号不存在");
						}
					}
					else if (opString.equals("设备编号")) {
						// 调用方法
						List<Device_Out> list= fillTableByCode(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该设备编号不存在");
						}
					}
					
					else if (opString.equals("出库日期")) {
						// 调用方法
						List<Device_Out> list= fillTableByDate(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该入库日期不存在");
						}
					}
				}
			}
		});
		button.setBounds(576, 81, 113, 27);
		contentPanel.add(button);

		txtout_id = new JTextField();
		txtout_id.setEditable(false);
		txtout_id.setColumns(10);
		txtout_id.setBounds(52, 124, 86, 30);
		contentPanel.add(txtout_id);

		txtdevice_code = new JTextField();
		txtdevice_code.setColumns(10);
		txtdevice_code.setBounds(138, 124, 86, 30);
		contentPanel.add(txtdevice_code);

		txtdepartment = new JTextField();
		txtdepartment.setColumns(10);
		txtdepartment.setBounds(223, 124, 86, 30);
		contentPanel.add(txtdepartment);

		txtout_date = new JTextField();
		txtout_date.setColumns(10);
		txtout_date.setBounds(309, 124, 86, 30);
		contentPanel.add(txtout_date);

		txtout_status = new JTextField();
		txtout_status.setColumns(10);
		txtout_status.setBounds(394, 124, 86, 30);
		contentPanel.add(txtout_status);

		txtout_num = new JTextField();
		txtout_num.setColumns(10);
		txtout_num.setBounds(480, 124, 86, 30);
		contentPanel.add(txtout_num);

		txtout_person = new JTextField();
		txtout_person.setColumns(10);
		txtout_person.setBounds(564, 124, 86, 30);
		contentPanel.add(txtout_person);

		txttaker = new JTextField();
		txttaker.setColumns(10);
		txttaker.setBounds(648, 124, 86, 30);
		contentPanel.add(txttaker);

		JButton button_1 = new JButton("确认修改");
		button_1.setFont(new Font("宋体", Font.PLAIN, 20));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取输入的值
				String tout_id = txtout_id.getText();
				String device_code = txtdevice_code.getText();
				String department = txtdepartment.getText();
				String out_date = txtout_date.getText();
				String out_status = txtout_status.getText();
				String tout_num = txtout_num.getText();
				String out_person = txtout_person.getText();
				String taker = txttaker.getText();
				// 转型
				int dout_id = Integer.parseInt(tout_id);
				int out_num = Integer.parseInt(tout_num);

				Device_OutDao doutd = new Device_OutDao();
				Device_Out dout = new Device_Out(dout_id, device_code, department, out_date, out_status, out_num,
						out_person, taker);
				// 调用修改的方法
				boolean flag = doutd.modDevOut(dout);
				if (flag) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "修改成功");
					fillTable(scrollPane);
				} else {
					JOptionPane.showMessageDialog(null, "修改失败");
				}
			}
		});
		button_1.setBounds(748, 126, 113, 27);
		contentPanel.add(button_1);

		// 创建表格
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 取值
				out_id = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String device_code = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String department = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				String out_date = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				String out_status = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String out_num = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
				String out_person = table.getValueAt(table.rowAtPoint(e.getPoint()), 6) + "";
				String taker = table.getValueAt(table.rowAtPoint(e.getPoint()), 7) + "";
				// 赋值
				txtout_id.setText(out_id);
				txtdevice_code.setText(device_code);
				txtdepartment.setText(department);
				txtout_date.setText(out_date);
				txtout_status.setText(out_status);
				txtout_num.setText(out_num);
				txtout_person.setText(out_person);
				txttaker.setText(taker);
			}
		});
		table.setRowHeight(25);

		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("删除");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 让用户选择
				int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！", "删除出库单号为: " + out_id + " 的所有信息",
						JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					// 获取单号
					Device_OutDao dod = new Device_OutDao();
					int outId = Integer.parseInt(out_id);
					// 调用方法
					boolean flag = dod.delDevOut(outId);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
					}
					// 更新
					fillTable(scrollPane);
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);
		fillTable(scrollPane);
		
		JButton button_2 = new JButton("取消");
		button_2.setFont(new Font("宋体", Font.PLAIN, 20));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInput.setText("");
				fillTable(scrollPane);
			}
		});
		button_2.setBounds(703, 81, 113, 27);
		contentPanel.add(button_2);

		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * 查询所有
	 * 
	 * @param scrollPane
	 */
	private void fillTable(JScrollPane scrollPane) {
		// 创建对象
		Device_OutDao dod = new Device_OutDao();
		// 调用查找所有的方法
		List<Device_Out> list = dod.findDevOutAll();
		int row = list.size();
		Object[][] obj = new Object[row][8];
		// 循环遍历
		for (int i = 0; i < obj.length; i++) {
			Device_Out dout = list.get(i);
			// 赋值
			obj[i][0] = dout.getOut_id();
			obj[i][1] = dout.getDevice_code();
			obj[i][2] = dout.getDepartment();
			obj[i][3] = dout.getOut_date();
			obj[i][4] = dout.getOut_status();
			obj[i][5] = dout.getOut_num();
			obj[i][6] = dout.getOut_person();
			obj[i][7] = dout.getTaker();
		}

		table.setModel(new DefaultTableModel(obj,
				new String[] { "\u51FA\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u4F7F\u7528\u90E8\u95E8",
						"\u51FA\u5E93\u65E5\u671F", "\u51FA\u5E93\u72B6\u6001", "\u51FA\u5E93\u6570\u91CF",
						"\u4ED3\u5E93\u5458\u5DE5", "\u9886\u53D6\u4EBA" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 根据单号查询
	 * 
	 * @param scrollPane
	 */
	private Device_Out fillTableById(JScrollPane scrollPane, int outId) {
		// 创建对象
		Device_OutDao dod = new Device_OutDao();
		// 根据单号
		Device_Out dout = dod.findDevOutById(outId);
		if (dout!=null) {
			Object[][] obj = new Object[1][8];
			// 循环遍历
			// 赋值
			obj[0][0] = dout.getOut_id();
			obj[0][1] = dout.getDevice_code();
			obj[0][2] = dout.getDepartment();
			obj[0][3] = dout.getOut_date();
			obj[0][4] = dout.getOut_status();
			obj[0][5] = dout.getOut_num();
			obj[0][6] = dout.getOut_person();
			obj[0][7] = dout.getTaker();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u51FA\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u4F7F\u7528\u90E8\u95E8",
							"\u51FA\u5E93\u65E5\u671F", "\u51FA\u5E93\u72B6\u6001", "\u51FA\u5E93\u6570\u91CF",
							"\u4ED3\u5E93\u5458\u5DE5", "\u9886\u53D6\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		
		return dout;
	}
	
	/**
	 * 根据设备编号查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_Out> fillTableByCode(JScrollPane scrollPane,String devCode) {
		// 创建对象
		Device_OutDao dod = new Device_OutDao();
		// 调用查找所有的方法
		List<Device_Out> list = dod.findDevOutByCode(devCode);
		if (list!=null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Out dout = list.get(i);
				// 赋值
				obj[i][0] = dout.getOut_id();
				obj[i][1] = dout.getDevice_code();
				obj[i][2] = dout.getDepartment();
				obj[i][3] = dout.getOut_date();
				obj[i][4] = dout.getOut_status();
				obj[i][5] = dout.getOut_num();
				obj[i][6] = dout.getOut_person();
				obj[i][7] = dout.getTaker();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u51FA\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u4F7F\u7528\u90E8\u95E8",
							"\u51FA\u5E93\u65E5\u671F", "\u51FA\u5E93\u72B6\u6001", "\u51FA\u5E93\u6570\u91CF",
							"\u4ED3\u5E93\u5458\u5DE5", "\u9886\u53D6\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		
		return list;
	}
	
	/**
	 * 根据设备编号查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_Out> fillTableByDate(JScrollPane scrollPane,String outDate) {
		// 创建对象
		Device_OutDao dod = new Device_OutDao();
		// 调用查找所有的方法
		List<Device_Out> list = dod.findDevOutByDate(outDate);
		if (list!=null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Out dout = list.get(i);
				// 赋值
				obj[i][0] = dout.getOut_id();
				obj[i][1] = dout.getDevice_code();
				obj[i][2] = dout.getDepartment();
				obj[i][3] = dout.getOut_date();
				obj[i][4] = dout.getOut_status();
				obj[i][5] = dout.getOut_num();
				obj[i][6] = dout.getOut_person();
				obj[i][7] = dout.getTaker();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u51FA\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u4F7F\u7528\u90E8\u95E8",
							"\u51FA\u5E93\u65E5\u671F", "\u51FA\u5E93\u72B6\u6001", "\u51FA\u5E93\u6570\u91CF",
							"\u4ED3\u5E93\u5458\u5DE5", "\u9886\u53D6\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		
		return list;
	}

	/**
	 * 右键
	 * 
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
